﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Toolkit.Web.Infastructure;
using Toolkit.Web.ExtensionMethods;
using System.Web;

namespace Toolkit.Web.ViewComponents.DataGrid
{
    public interface IReadOnlyDataGridColumn<T> where T : class
    {
        IReadOnlyDataGridColumnSettings Settings
        {
            get;
        }

        IHtmlString Label
        {
            get;
        }

        DataGridFilterHelper<T> FilterHelper
        {
            get;
        }

        IList<IReadOnlyDataGridColumnFilter> Filters
        {
            get;
        }
    }

    public sealed class DataGridColumn<T> : IReadOnlyDataGridColumn<T> where T : class
    {
        private IHtmlString _label;

        public DataGridColumn(DataGrid<T> dataGrid, string key)
        {
            if (dataGrid == null)
            {
                throw new ArgumentNullException("dataGrid");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("key");
            }

            DataGrid = dataGrid;
            Identifier = Guid.NewGuid();
            HtmlAttributes = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            HeaderHtmlAttributes = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            FooterHtmlAttributes = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            Settings = new DataGridColumnSettings(key);
            FilterHelper = new DataGridFilterHelper<T>(this);
        }

        public DataGrid<T> DataGrid
        {
            get;
            private set;
        }

        public Guid Identifier
        {
            get;
            private set;
        }

        public IDictionary<string, object> HtmlAttributes
        {
            get;
            private set;
        }

        public IDictionary<string, object> HeaderHtmlAttributes
        {
            get;
            private set;
        }

        public IDictionary<string, object> FooterHtmlAttributes
        {
            get;
            private set;
        }

        public DataGridColumnSettings Settings
        {
            get;
            set;
        }

        IReadOnlyDataGridColumnSettings IReadOnlyDataGridColumn<T>.Settings
        {
            get
            {
                if (Settings != null)
                {
                    return (IReadOnlyDataGridColumnSettings)Settings;
                }
                return null;
            }
        }

        public IHtmlString Label
        {
            get
            {
                if (_label == null)
                {
                    if (HeaderTemplate != null && HeaderTemplate.HasValue())
                    {
                        _label = HtmlStringExtensions.Create(HttpUtility.HtmlEncode(HeaderTemplate.ToHtmlString().RemoveHtmlTags()));
                    }
                    else if (DataGrid != null && DataGrid.DefaultColumnHeaderTemplate != null && DataGrid.DefaultColumnHeaderTemplate.HasValue())
                    {
                        _label = HtmlStringExtensions.Create(HttpUtility.HtmlEncode(DataGrid.DefaultColumnHeaderTemplate.ToHtmlString().RemoveHtmlTags()));
                    }
                    else
                    {
                        _label = HtmlStringExtensions.Create(string.Empty);
                    }
                }

                return _label;
            }
            set
            {
                _label = value;
            }
        }

        public HtmlTemplate HeaderTemplate
        {
            get;
            set;
        }

        public HtmlTemplate<T> Template
        {
            get;
            set;
        }

        public HtmlTemplate<T> EditorTemplate
        {
            get;
            set;
        }

        public HtmlTemplate FooterTemplate
        {
            get;
            set;
        }

        public Action<DataGridColumnFilterFactory<T>> FilterFactory
        {
            get;
            set;
        }

        public IList<DataGridColumnFilter> Filters
        {
            get;
            set;
        }

        IList<IReadOnlyDataGridColumnFilter> IReadOnlyDataGridColumn<T>.Filters
        {
            get
            {
                if (Filters != null)
                {
                    return ((IEnumerable<IReadOnlyDataGridColumnFilter>)Filters).ToList();
                }
                return null;
            }
        }

        public HtmlTemplate<IReadOnlyDataGridColumn<T>> FilterSummaryTemplate
        {
            get;
            set;
        }

        public DataGridFilterHelper<T> FilterHelper
        {
            get;
            private set;
        }

        public bool IsFilterable
        {
            get
            {
                return (
                    Filters != null &&
                    Filters.Count > 0
                );
            }
        }
    }
}
